package com.tyx.chargingpile.service.impl;

import com.tyx.chargingpile.Enum.ServiceMessageEume;
import com.tyx.chargingpile.cache.SocketChannelMap;
import com.tyx.chargingpile.service.ChargingPileService;
import com.tyx.chargingpile.service.MessageService;
import com.tyx.chargingpile.util.Util;
import com.tyx.chargingpile.vo.PileInfo;
import com.tyx.chargingpile.vo.SocketResponseResult;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 卡鉴权结果返回下行
 */
@Service("chargingPileCardMsgReportCheckResultDown")
public class ChargingPileCardMsgReportCheckResultDown implements ChargingPileService {

    private final static Logger LOGGER = LoggerFactory.getLogger(ChargingPileCardMsgReportCheckResultDown.class);

    @Override
    public SocketResponseResult execute(ChannelHandlerContext channelHandlerContext, byte[] context) throws Exception {
        return null;
    }

    @Override
    public SocketResponseResult execute(ChannelHandlerContext channelHandlerContext, Map data) throws Exception {
        String pileNum = (String) data.get("PileId");
        String gunpointNum = (String) data.get("ChargePortIndex");
        String cardNo = (String) data.get("CardId");
        String physicalId = (String) data.get("PhysicalId");
        String amountMoney = (String) data.get("AmountMoney");
        String authResult = (String) data.get("AuthResult");
        String errorCode = (String) data.get("ErrorCode");
        String locakStatus = (String) data.get("LocakStatus");
        String powerType = SocketChannelMap.getPileInfo(pileNum).getPowerType();
        // 数据往前补0
        gunpointNum = Util.FronFillZero(gunpointNum,2);
        pileNum = Util.FronFillZero(pileNum,12);
        // 把amountMoney转化为分的单位，补够6位数
        amountMoney = Util.formatDoubleTOInt(Double.parseDouble(amountMoney),100) + "";
        amountMoney = Util.FronFillZero(amountMoney,8);

        cardNo = Util.FronFillZero(cardNo,12);

        Map<String,String> dwn = new HashMap<>();
        dwn.put("gunpointNum",gunpointNum);
        dwn.put("powerType",powerType);
        dwn.put("pileNum",pileNum);
        dwn.put("cardNo",cardNo);
        dwn.put("physicalCardNo",physicalId);
        dwn.put("cardBalance",amountMoney);
        if (locakStatus.equals("1")){
            dwn.put("cardStatus","FF");
        }else {
            dwn.put("cardStatus","00");
        }
        if (authResult.equals("1")){
            dwn.put("checkResult","FF");
        }else {
            dwn.put("checkResult","00");
        }
        dwn.put("suggest",errorCode);
        byte[] dwnContext = MessageService.getMessage(dwn,ServiceMessageEume.REPORTCHECKRESULT.getDwnLinePath());
        ByteBuf pingMessage = Unpooled.buffer();
        pingMessage.writeBytes(dwnContext);
        channelHandlerContext.writeAndFlush(pingMessage);
        String commandId = ServiceMessageEume.REPORTCHECKRESULT.getCode();
        LOGGER.info("客户端"+channelHandlerContext.channel().id()+",指令控制编号："+commandId+",桩编号:" + pileNum + ",前置卡鉴权结果返回下行报文" + Util.BCD2String(dwnContext));
        return new SocketResponseResult(channelHandlerContext,dwnContext);
    }
}
